package com.shuziren.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuziren.domain.CandidateEntity;
import com.shuziren.domain.InterPersonEntity;
import com.shuziren.entity.InterviewPosition;
import com.shuziren.mapper.InterviewRecordMapper;
import com.shuziren.pojo.dto.InterviewRecordDto;
import com.shuziren.pojo.entity.InterviewRecord;
import com.shuziren.pojo.vo.InterviewRecordVo;
import com.shuziren.service.CandidateService;
import com.shuziren.service.InterPersonService;
import com.shuziren.service.InterviewPositionService;
import com.shuziren.service.InterviewRecordService;
import com.shuziren.abnormal.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class InterviewRecordServiceImpl extends ServiceImpl<InterviewRecordMapper, InterviewRecord> implements InterviewRecordService {

    @Autowired
    private InterviewRecordMapper interviewRecordMapper;
    @Autowired
    private CandidateService candidateService;
    @Autowired
    private InterviewPositionService interviewPositionService;
    @Autowired
    private InterPersonService interPersonService;

    // 查询单个记录
    @Override
    public InterviewRecordVo getInterviewRecordById(Integer id) throws CustomException {
        InterviewRecord record2 = getById(id);
        if (record2 == null) {
            throw new CustomException("查询的面试记录不存在。");
        }
        InterviewRecordVo record = new InterviewRecordVo();
        record.setId(record2.getId());
        record.setCandidateId(record2.getCandidateId());
        record.setCandidateName(candidateService.getById(record2.getCandidateId()).getName());
        record.setPositionId(record2.getPositionId());
        record.setPositionName(interviewPositionService.getById(record2.getPositionId()).getPositionName());
        record.setPersonId(record2.getPersonId());
        record.setPersonName(interPersonService.getById(record2.getPersonId()).getName());
        record.setScheduleId(record2.getScheduleId());
        record.setScore(record2.getScore());
        record.setComments(record2.getComments());
        record.setCreatedAt(record2.getCreatedAt());
        record.setUpdatedAt(record2.getUpdatedAt());
        return record;
    }

    // 新增记录
    @Override
    public boolean saveInterviewRecord(InterviewRecord record) throws CustomException {
        boolean result = save(record);
        if (!result) {
            throw new CustomException("保存面试记录失败。");
        }
        return result;
    }

    // 更新记录
    @Override
    public boolean updateInterviewRecord(InterviewRecord record) throws CustomException {
        boolean result = updateById(record);
        if (!result) {
            throw new CustomException("更新面试记录失败。");
        }
        return result;
    }

    // 删除记录
    @Override
    public boolean deleteInterviewRecord(Integer id) throws CustomException {
        boolean result = removeById(id);
        if (!result) {
            throw new CustomException("删除面试记录失败。");
        }
        return result;
    }


    // 分页查询
    @Override
    public IPage<InterviewRecordVo> getAllInterviewRecordsPage(InterviewRecordDto queryVo) {
        IPage<InterviewRecord> page = new Page<>(queryVo.getPageNum(), queryVo.getPageSize());
        IPage<InterviewRecord> interviewRecordIPage = interviewRecordMapper.selectPage(page, null);

        List<InterviewRecordVo> interviewRecordVo = new ArrayList<>();
        List<InterviewRecord> records = interviewRecordIPage.getRecords();
        records.forEach(interviewRecord -> {
            InterviewRecordVo vo = new InterviewRecordVo();
            vo.setId(interviewRecord.getId());
            vo.setCandidateId(interviewRecord.getCandidateId());
            vo.setPositionId(interviewRecord.getPositionId());
            vo.setPersonId(interviewRecord.getPersonId());
            vo.setScheduleId(interviewRecord.getScheduleId());
            vo.setScore(interviewRecord.getScore());
            vo.setComments(interviewRecord.getComments());
            vo.setCreatedAt(interviewRecord.getCreatedAt());
            vo.setUpdatedAt(interviewRecord.getUpdatedAt());

            // 安全获取关联信息
            vo.setCandidateName(Optional.ofNullable(candidateService.getById(interviewRecord.getCandidateId()))
                    .map(CandidateEntity::getName)
                    .orElse("未知候选人"));

            vo.setPositionName(Optional.ofNullable(interviewPositionService.getById(interviewRecord.getPositionId()))
                    .map(InterviewPosition::getPositionName)
                    .orElse("未知职位"));

            vo.setPersonName(Optional.ofNullable(interPersonService.getById(interviewRecord.getPersonId()))
                    .map(InterPersonEntity::getName)
                    .orElse("未知面试官"));

            interviewRecordVo.add(vo);
        });

        Page<InterviewRecordVo> interviewRecordVOPage = new Page<>();
        interviewRecordVOPage.setCurrent(page.getCurrent());
        interviewRecordVOPage.setSize(page.getSize());
        interviewRecordVOPage.setTotal(page.getTotal());
        interviewRecordVOPage.setRecords(interviewRecordVo);
        return interviewRecordVOPage;
    }
}
